<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="org.dualr.litelog.entity.Category"%>
<%@ page import="org.dualr.litelog.dao.CategoryDao"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Category - LiteLog Administration</title>
<link href="../admin/common.css" rel="stylesheet" type="text/css" media="screen"/>
<script type="text/javaScript">
	function edit(id, name, type, parentId, counts){
		document.getElementById("edit_id").value = id;
		document.getElementById("edit_name").value = name;
		document.getElementById("edit_type").value = type;
		document.getElementById("edit_parentId").value = parentId;
		document.getElementById("edit_counts").value = counts;
	}
	function submit(){
		var name = document.getElementById("edit_name").value;
		if (category == ""){
			alert("请填写分类名");
			return;
		}
		 document.edit_form.submit();
	}
</script>
</head>
<body>
<h1>分类管理</h1>
<div>
<%
	List<Category> categoryList = CategoryDao.getInstance().getAll();
%>
	<form action="../admin?entity=category&option=add" method="post">
			类名:<input type="text" name="name" />
			类型:<select name="type">
				<option value="post">post</option>
				<option value="photo">photo</option>
				<option value="link">link</option>
			</select>
			父类:<select name="parentId">
				<option value="0">根类</option>
				<%
					if (categoryList.size() != 0){
										for(Category category : categoryList){
				%>
							<option value="<%=category.getId()%>"><%=category.getName()%></option>
							<%
								}
																
															}
							%>
			</select>
			
			&nbsp;&nbsp;<input type="submit" value="添加" ></input>
	
	</form>
</div>
		<div>
			<form action="../admin?entity=category&option=update" method="post" name="edit_form">
			<input type="hidden" name="id" id="edit_id" value=""/>
			类名:<input type="text" name="name" id="edit_name"/>
			类型:<select name="type" id="edit_type">
				<option value="post">post</option>
				<option value="photo">photo</option>
				<option value="link">link</option>
			</select>
			父类:<select name="parentId" id="edit_parentId">
				<option value="0">根类</option>
				<%
					if (categoryList.size() != 0){
										for(Category category : categoryList){
				%>
							<option value="<%=category.getId()%>"><%=category.getName()%></option>
							<%
						}
						
					}
				%>
			</select>
			记录数<input type="text" name="counts" id="edit_counts"/>
			&nbsp;&nbsp;<input type="submit" value="保存" />
	</form>
	</div>
	
	<div>
	<table class="data-list" width="100%" cellspacing="0" cellpadding="0">
	<tr class="caption">
		<td width="28">序号</td>
		<td width="28">ID</td>
		<td width="80">名称</td>
		<td width="50">类型</td>
		<td width="100">父类</td>
		<td width="40">记录数</td>
		<td width="30">编辑</td>
		<td width="30">删除</td>
	</tr>
	<%
		if (categoryList.size() != 0){
			int i = 1;
			for(Category category : categoryList){
	%>
	<tr>
		<td><%=i++%></td>
		<td><%=category.getId()%></td>
		<td><%=category.getName()%></td>
		<td><%=category.getType()%></td>
		<td>
			<%
				if(category.getParentId() == 0){
					%>
					根类
					<%
				}else{
					for(Category categorys : categoryList){
						if(category.getParentId() == categorys.getId()){
							out.print(categorys.getName());
						}
					}
				}
			%>
		</td>
		<td><%=category.getCounts()%></td>
		<td><a href="javaScript:edit('<%=category.getId()%>','<%=category.getName()%>', '<%=category.getType()%>', '<%=category.getParentId()%>', '<%=category.getCounts()%>')">编辑</a></td>
		<td><a href="/admin?entity=category&option=delete&id=<%=category.getId()%>">删除</a></td>
	</tr>
	<%
					}
								}
				%>
	</table>
	</div>
</body>
</html>